package com.first.group.newhouse.service.impl;

import com.first.group.common.utils.R;
import com.first.group.newhouse.service.NewHouseImgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.first.group.common.utils.PageUtils;
import com.first.group.common.utils.Query;

import com.first.group.newhouse.dao.NewHouseDao;
import com.first.group.newhouse.entity.NewHouseEntity;
import com.first.group.newhouse.service.NewHouseService;


@Service("newHouseService")
public class NewHouseServiceImpl extends ServiceImpl<NewHouseDao, NewHouseEntity> implements NewHouseService {

    @Autowired
    private NewHouseImgService newHouseImgService;

    @Autowired
    private NewHouseDao newHouseDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<NewHouseEntity> page = this.page(
                new Query<NewHouseEntity>().getPage(params),
                new QueryWrapper<NewHouseEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<NewHouseEntity> findList() {
        List<NewHouseEntity> newHouseEntities = newHouseDao.selectList(null);
        return newHouseEntities;
    }

    @Override
    public R updateStatus(Long newHouseId) {

        //修改状态
        //根据当前的id查询所有的数据
        NewHouseEntity newHouseEntity = baseMapper.selectById(newHouseId);
        //判断当前的状态

        if (newHouseEntity.getNewHouseState() == 1) {
            Integer status = 0;
            newHouseEntity.setNewHouseState(status);
            baseMapper.updateById(newHouseEntity);
            return R.ok();
        }
        if (newHouseEntity.getNewHouseState() == 0) {
            Integer status = 1;
            newHouseEntity.setNewHouseState(status);
            baseMapper.updateById(newHouseEntity);
            return R.ok();
        }

        return R.ok();
    }

    @Override
    public R saveNewHouse(NewHouseEntity newHouse) {
        //添加楼盘表
        //首先添加新房列表
        newHouse.setNewHouseState(1);
        baseMapper.insert(newHouse);
        //添加图片表,批量添加
        newHouseImgService.saveImages(newHouse.getNewHouseId(),newHouse.getFileList());
        return R.ok();
    }

}